package br.com.engine.componentes;

import java.util.ArrayList;
import java.util.List;

import br.com.engine.Vector2;
import br.com.engine.interfaces.IComponent;
import br.com.engine.interfaces.IScript;

public class GameObject implements IScript
{
	private Vector2 position;
	
	private List<IComponent> componentes;
	private List<IScript>    scripts;

	public GameObject( )
	{
		componentes = new ArrayList<IComponent>( );
		scripts     = new ArrayList<IScript>( );
		position    = new Vector2( );
	}
	
	public Vector2 getPosition( )
	{
		return position;
	}

	public void setPosition( Vector2 position )
	{
		this.position = position;
	}

	public List<IComponent> getComponentes( )
	{
		return componentes;
	}

	public void addComponente( IComponent componentes ) 
	{
		this.componentes.add( componentes );
	}
	
	public List<IScript> getScripts( )
	{
		return scripts;
	}

	public void addScripts( IScript script ) 
	{
		this.scripts.add( script );
	}

	@Override
	public void update( long time ) 
	{
		scripts.forEach( sc ->
		{
			sc.update( time );
		} );
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getComponent( Class<T> item )
	{
		IComponent obj = null;
		
		for( IComponent comp : componentes )
		{
			if( item.isInstance( comp ) )
			{
				obj = comp;
			}
		}
		
		return (T)obj;
	}
}